/**
 * created by 小卷毛, 2021-07-15
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
@file:Suppress("HasPlatformType")

package com.rzico.sbl.other

import android.os.SystemClock
import android.view.MotionEvent
import android.widget.RadioButton
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ContentReportManagerTimeBinding
import com.xinnuo.common.helper.TimeHelper
import com.xinnuo.common.helper.longValue
import com.xinnuo.common.helper.startRotateAnimator
import com.xinnuo.common.helper.toFormat
import com.xinnuo.common.view.setCheckedChangeListener
import com.xinnuo.common_ui.utils.showTimeRangeDialog

/**
 * 描 述：
 * 作 者：小卷毛  2021-07-15 15:58
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */

// 今天的时间
fun getTodayDate() =
    TimeHelper
        .getInstance()
        .stringDateShort

// 昨天的时间
fun getBeforeDate() =
    TimeHelper
        .getInstance()
        .getNextDay(getTodayDate(), -1)

// 几天之前的时间
fun getBeforeDate(day: Int, pattern: String = "yyyy-MM-dd") =
    TimeHelper
        .getInstance()
        .getNextDay(getTodayDate(), day * -1, "yyyy-MM-dd")
        .toFormat("yyyy-MM-dd", pattern)

// 本周一的时间
fun getMondayDate() =
    TimeHelper
        .getInstance()
        .mondayOFWeek
        .toFormat(sFormat = "yyyy年MM月dd日")

// 本周日的时间
fun getSundayDate() =
    TimeHelper
        .getInstance()
        .sundayOFWeek
        .toFormat(sFormat = "yyyy年MM月dd日")

// 本月第一天的时间
fun getFirstDate() =
    TimeHelper
        .getInstance()
        .dayOfMonthFirst

// 本月最后一天的时间
fun getEndDate() =
    TimeHelper
        .getInstance()
        .dayOfMonthEnd

// 上月第一天的时间
fun getBeforeFirstDate() =
    TimeHelper
        .getInstance()
        .previousMonthFirst

// 上月最后一天的时间
fun getBeforeEndDate() =
    TimeHelper
        .getInstance()
        .previousMonthEnd

// 本年第一天的时间
fun getYearFirstDate() =
    TimeHelper
        .getInstance()
        .getAnyYearFirst(0)

// 本年最后一天的时间
fun getYearEndDate() =
    TimeHelper
        .getInstance()
        .getAnyYearEnd(0)

// 指定时间和当前时间差
fun getDiffTime(milliseconds: String) = System.currentTimeMillis() - milliseconds.longValue

// 格式化时间差
fun formatTime(milliseconds: Long): String {
    val min = 60 * 1000
    val hou = min * 60

    val hour = milliseconds / hou
    val minute = (milliseconds - hour * hou) / min + 1

    return (if (hour > 0) "${hour}时" else "") + "${minute}分"
}

/**
 * 总经理时间筛选统一方法
 */
inline fun ContentReportManagerTimeBinding.addTimeListener(
    crossinline onTimeSelected: (String, String, String) -> Unit = { _, _, _ -> }
) {
    rgTime.setCheckedChangeListener {
        onCheckedChanged { group, checkId ->
            val button = group.findViewById<RadioButton>(checkId)

            when (button.id) {
                R.id.rb_time1 -> {
                    rbTime3.text = "自定义时间"
                    rbTime3Arrow.setImageResource(R.mipmap.icon_arrow_down_999)

                    onTimeSelected.invoke(getBeforeDate(6), getTodayDate(), "近7天")
                }

                R.id.rb_time2 -> {
                    rbTime3.text = "自定义时间"
                    rbTime3Arrow.setImageResource(R.mipmap.icon_arrow_down_999)

                    onTimeSelected.invoke(getFirstDate(), getEndDate(), "本月")
                }

                R.id.rb_time4 -> {
                    rbTime3.text = "自定义时间"
                    rbTime3Arrow.setImageResource(R.mipmap.icon_arrow_down_999)

                    onTimeSelected.invoke(getBeforeFirstDate(), getBeforeEndDate(), "上月")
                }
            }
        }
    }

    var lastClickTime = 0L

    rbTime3Ll.setOnTouchListener { view, event ->

        when (event.action) {
            MotionEvent.ACTION_UP -> {
                val diff = SystemClock.elapsedRealtime() - lastClickTime
                if (diff > 1000) {
                    lastClickTime = SystemClock.elapsedRealtime()

                    view.context.showTimeRangeDialog(
                        minYearValue = 2021,
                        isCurrentDate = true,
                        isAfterLimited = true,
                        onShow = { rbTime3Arrow.startRotateAnimator(0f, 180f) },
                        onDismiss = { rbTime3Arrow.startRotateAnimator(180f, 0f) },
                        onResult = { start, end ->
                            rbTime3.text = "$start ~ $end"
                            rbTime3.isChecked = true
                            rbTime3Arrow.setImageResource(R.mipmap.icon_arrow_down_blue)

                            onTimeSelected.invoke(start, end, "$start ~ $end")
                        }
                    )
                }
            }
        }

        return@setOnTouchListener true
    }
}